package org.vse.aha.gui.windows;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.AbstractAction;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.AbstractNode;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import org.vse.aha.common.quotes.IQuotesProvider;
import org.vse.aha.common.quotes.Quote;
import org.vse.aha.gui.nodes.QuoteNode;
import org.vse.aha.gui.nodes.QuoteNodeFactory;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.vse.aha.gui.windows//Vyroky//EN",
autostore = false)
@TopComponent.Description(preferredID = "QuotesTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "org.vse.aha.gui.windows.QuotesTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_QuotesAction",
preferredID = "QuotesTopComponent")
@Messages({
  "CTL_QuotesAction=Výroky",
  "CTL_QuotesTopComponent=Výroky Window",
  "HINT_QuotesTopComponent=This is a Výroky window"
})
public final class QuotesTopComponent extends TopComponent implements ExplorerManager.Provider {
  private ExplorerManager em;
  
  public QuotesTopComponent() {
    initComponents();
    setName(Bundle.CTL_QuotesTopComponent());
    setToolTipText(Bundle.HINT_QuotesTopComponent());

    // Vytvoříme nové view a nastavíme popisek prvního sloupce
    OutlineView ov = new OutlineView("Autor");
    ov.setPropertyColumns(QuoteNode.TEXT_PROPERTY, "Text");
    this.em = new ExplorerManager();
    
    // Získáme všechny výroky všech poskytovatelů
    Collection<? extends IQuotesProvider> pvc = Lookup.getDefault().lookupAll(IQuotesProvider.class);
    ArrayList<Quote> výroky = new ArrayList<>();
    for (IQuotesProvider pv : pvc) {
      výroky.addAll(pv.getVýroky());
    }
     
    // Vytvoříme kořenový uzel a jako parametr dostane od továrny
    // vytvořené všechny potomky, uzly výroků
    AbstractNode rootNode = new AbstractNode(new QuoteNodeFactory(výroky));
    rootNode.setDisplayName("Všechny");
    this.em.setRootContext(rootNode);
    
    // Umístíme view do okna
    setLayout(new BorderLayout());
    add(ov, BorderLayout.CENTER);
    
    // Umístíme objekty do Lookup (kvůli oknu Properties atp.)
    associateLookup(ExplorerUtils.createLookup(em, getActionMap()));
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
  @Override
  public void componentOpened() {
    // TODO add custom code on component opening
  }

  @Override
  public void componentClosed() {
    // TODO add custom code on component closing
  }

  void writeProperties(java.util.Properties p) {
    // better to version settings since initial version as advocated at
    // http://wiki.apidesign.org/wiki/PropertyFiles
    p.setProperty("version", "1.0");
    // TODO store your settings
  }

  void readProperties(java.util.Properties p) {
    String version = p.getProperty("version");
    // TODO read your settings according to their version
  }

  @Override
  public ExplorerManager getExplorerManager() {
    return em;
  }
}
